package com.martin.lib_base.ui.screen

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun PointIndicator(pageCount: Int, pagerState: PagerState) {
    val currentPage by rememberUpdatedState(newValue = pagerState.currentPage)

    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        for (i in 0 until pageCount) {
            if (i > 0) {
                Box(modifier = Modifier.width(5.dp))
            }
            Point(
                selected = (currentPage % pageCount) == i
            )
        }
    }
}

@Composable
private fun Point(selected: Boolean) {
    Box(
        modifier = Modifier
            .size(6.dp)
            .background(
                color = if (selected) Color.White else Color(0x66FFFFFF),
                shape = CircleShape
            )
    )
}
